package com.xgry.bpm.common.utils;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: CN-xin
 * @Date: 2019-10-26 09:32
 * @Description:
 */
@Data
public class MiniUITreeUtil {

    //节点id
    private Integer id;
    //父节点id
    private String parenIid;
    //节点名称
    private String text;
    //图标
    private String iconCls;
    //url
    private String url;

    private String other;
//区分组织和部门
    private String ooo;
    //子节点集合
    private List<MiniUITreeUtil> children;

    //表名
    private String tableName;
    //路由
    private String vsystemid;


    public MiniUITreeUtil() {

    }

    public MiniUITreeUtil(Integer id, String parenIid, String text, String iconCls, String url, String tableName, String vsystemid) {
        this.id = id;
        this.parenIid = parenIid;
        this.text = text;
        this.iconCls = iconCls;
        this.url = url;
        this.tableName = tableName;
        this.vsystemid = vsystemid;

    }

    public MiniUITreeUtil(Integer id, String parenIid, String text, String iconCls, String url) {
        this.id = id;
        this.parenIid = parenIid;
        this.text = text;
        this.iconCls = iconCls;
        this.url = url;
    }
    public MiniUITreeUtil(Integer id, String parenIid, String text, String iconCls, String url, String other) {
        this.id = id;
        this.parenIid = parenIid;
        this.text = text;
        this.iconCls = iconCls;
        this.url = url;
        this.other= other;
    }

    /**
     * 递归（先得到父节点）再递归
     *
     * @param list
     * @return
     */
    public static List<MiniUITreeUtil> getTreeResult(List<MiniUITreeUtil> list) {
        List<MiniUITreeUtil> listResult = new ArrayList<>();
        for (MiniUITreeUtil t : list) {
            if (t.getParenIid().equals("-1" )) {
                listResult.add(t);// 得到父类
            }
        }
        secondTree(list, listResult);
        return listResult;
    }

    /**
     * 父节点获取子节点
     *
     * @param list
     * @param listResult
     * @return
     */

    public static void secondTree(List<MiniUITreeUtil> list, List<MiniUITreeUtil> listResult) {
        for (MiniUITreeUtil tResult : listResult) {
            List<MiniUITreeUtil> childrens = new ArrayList<>();
            for (MiniUITreeUtil t : list) {
                if (t.getParenIid().equals("-1" )) {
                    continue;
                }
                if (tResult.getId().equals(t.getParenIid())) {
                    childrens.add(t);
                }
            }
            tResult.setChildren(childrens);
            if (!tResult.getChildren().isEmpty()) {
                secondTree(list, tResult.getChildren());
            }
        }

    }

    //使用方法参考
  /*  List< MiniUITreeUtil> tree = new ArrayList<>();
        for (EventType e:list //原来的数据进行转换，list是原来的数据
                ) {        
        tree.add(new MiniUITreeUtil(e.getName(), e.getEventTypeId(), e.getParentId()));
    }
    List<MiniUITreeUtil> result =  MiniUITreeUtil.getTreeResult(tree);     //递归得到的结果
    */
}
